package ShopController

import (
	"QingDai-Gin/api/entities"
	"QingDai-Gin/api/models"
	"QingDai-Gin/service/ShopService"
	"QingDai-Gin/utils/JwtUtil"
	"github.com/gin-gonic/gin"
)

// GetAllShop
// @Tags 商店
// @Summary 获取所有商店
// @Router /shop/ [get]
func GetAllShop(context *gin.Context) {
	allShop, _ := ShopService.GetAllShop()
	context.JSON(200, entities.CommonResult{Code: 200, Message: "获取所有商店成功", Data: allShop})
}

// GetShopByJwt
// @Tags 商店
// @Summary 根据Jwt获取本人商店
// @Router /shop/jwt [get]
func GetShopByJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	shops, _ := ShopService.GetShopsByP(map[string]any{"seller_id": UserId})
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据Jwt获取本人商店成功", Data: shops})
}

// CreateShopByJwt
// @Tags 商店
// @Summary 根据Jwt创建商店
// @Router /shop/jwt [post]
func CreateShopByJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	name, _ := context.Get("name")
	shop := models.Shop{
		Name:                name.(string),
		SecurityFund:        0,
		Score:               5,
		SellerId:            UserId,
		CustomerServiceList: "",
	}
	ShopService.CreateShop(shop)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据Jwt创建商店成功", Data: nil})
}

// GetShopById
// @Tags 商店
// @Summary 根据id获取商店
// @Router /shop/id [get]
func GetShopById(context *gin.Context) {
	id, _ := context.Get("id")
	shop, _ := ShopService.GetShopById(id.(int))
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据id获取商店成功", Data: shop})
}

// UpdateShopByIdAndJwt
// @Tags 商店
// @Summary 根据id及Jwt更改商店
// @Router /shop/jwt/id [put]
func UpdateShopByIdAndJwt(context *gin.Context) {
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据id及Jwt更改商店成功", Data: nil})
}

// DeleteShopByIdAndJwt
// @Tags 商店
// @Summary 根据id及Jwt删除商店
// @Router /shop/jwt/id [delete]
func DeleteShopByIdAndJwt(context *gin.Context) {
	UserId, err := JwtUtil.GetUserIdByContext(context)
	if err != nil {
		context.JSON(400, entities.CommonResult{Code: 400, Message: err.Error(), Data: nil})
	}
	id, _ := context.Get("id")
	shop, _ := ShopService.GetShopById(id.(int))
	if shop.SellerId != UserId {
		context.JSON(400, entities.CommonResult{Code: 400, Message: "不具备删除该商店权限", Data: nil})
	}
	ShopService.DeleteShop(shop)
	context.JSON(200, entities.CommonResult{Code: 200, Message: "根据id及Jwt删除商店成功", Data: nil})
}
